package com.prim.update.impl;

import android.content.Context;

import com.prim.update.UpdateManager;
import com.prim.update.listener.OnDownloadListener;
import com.prim.update.model.Progress;
import com.prim.update.notification.NotificationHelper;

import java.lang.ref.WeakReference;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：3/6 0006
 * 描    述：下载通知栏UI提示
 * 修订历史：
 * ================================================
 */
public class DefaultDownloadUINotificationHandler implements OnDownloadListener {
    private WeakReference<Context> mContext;
    private UpdateManager.Builder builder;
    private UpdateExplorer explorer;
    private static final String TAG = "DefaultDownloadUINotifi";

    public DefaultDownloadUINotificationHandler(Context mContext, UpdateExplorer explorer, UpdateManager.Builder builder) {
        this.mContext = new WeakReference<>(mContext);
        this.builder = builder;
        this.explorer = explorer;
    }

    private NotificationHelper createNotification(String text) {
        return NotificationHelper.getInstance()
                .with(mContext.get())
                .setNotifyId(builder.notificationId)
                .setContentTitle(builder.downloadTitle)
                .setContentText(text)
                .setLargeIcon(builder.largeIcon)
                .setSmallIcon(builder.smallIcon)
                .setNotifycation();
    }

    @Override
    public void onStart() {
        createNotification("下载开始");
    }

    @Override
    public void onProgress(Progress progress) {
        if (NotificationHelper.getInstance().isNotificationNull()) {
            createNotification("下载中...").updateNotification((int) (progress.fraction * 100), "下载中...");
        } else {
            NotificationHelper.getInstance().updateNotification((int) (progress.fraction * 100), "下载中...");
        }
    }

    @Override
    public void onError(String msg) {
        NotificationHelper.getInstance().updateNotification(-1, "下载错误");
    }

    @Override
    public void onFinish() {
        NotificationHelper.getInstance().updateNotification(-1, "下载完成");
    }
}
